package com.sigmatrix.h5.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sigmatrix.h5.components.FeignManager;
import com.sigmatrix.h5.entity.DealerWareHouse;
import com.sigmatrix.h5.entity.DisDealer;
import com.sigmatrix.h5.entity.Enterprise;
import com.sigmatrix.h5.service.DealerWareHouseService;
import com.sigmatrix.h5.service.EnterpriseService;
import com.sigmatrix.h5.utils.Assert;
import com.sigmatrix.h5.utils.Constants;
import com.sigmatrix.sc.jdbc.common.service.ServiceImpl;

/**
 * 
 *@ClassName: DealerWareHouseServiceImpl
 *@Description: 经销商仓库
 *@author MH
 *@date 2017年11月13日 下午7:14:46
 */
@Service
public class DealerWareHouseServiceImpl extends ServiceImpl<DealerWareHouse, Integer> implements DealerWareHouseService{
	
	@Autowired
	private FeignManager feignManager;
	
	@Autowired
	private EnterpriseService enterpriseService;
	
	@Override
	public Integer createDeafultWareHouse(DisDealer dealer) {
		DealerWareHouse house = new DealerWareHouse();
		house.setProvinceId(dealer.getProvince());
		house.setCityId(dealer.getCity());
		house.setCitySubOrdinate(dealer.getCitySubOrdinate());
		house.setAddress(dealer.getAddress());
		house.setName("其他仓库");
		house.setCode(code(getEseCode(dealer.getEnterpriseId())));//生成 code
		house.setDealerId(dealer.getId());
		house.setIsDeafult(Short.valueOf("1"));//1代表默认仓库
		house.setRemarks("经销商反向注册自动生成");
		house.setStatus(Constants.STATUS_ENABLE);
		house.setCreateTime(new Date());
		return insertSelective(house);
	}
	
	
	@Override
	public String code(String eseCode){
		Long sequence = feignManager.sequence(eseCode,"DWH", "");
		Assert.notNull(sequence, "生成sequence失败");
		StringBuffer buffer = new StringBuffer(sequence+"");
		while(buffer.length() <4){
			buffer.insert(0, "0");
		}
		return buffer.insert(0, "DWH").toString();
	}
	
	private String getEseCode(Integer eseId){
		Enterprise enterprise = enterpriseService.findById(eseId);
		Assert.notNull(enterprise, "eseId错误");
		return enterprise.getCode();
	}
}
